package com.seg.web.controller.wms;

import com.seg.common.annotation.Log;
import com.seg.common.core.controller.BaseController;
import com.seg.common.core.domain.AjaxResult;
import com.seg.common.core.page.TableDataInfo;
import com.seg.common.enums.BusinessType;
import com.seg.common.utils.poi.ExcelUtil;
import com.seg.system.domain.TInWarehouse;
import com.seg.system.service.ITInWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 备件入库Controller
 *
 * @author seg
 * @date 2023-07-04
 */
@RestController
@RequestMapping("/wms/in")
public class TInWarehouseController extends BaseController {
    @Autowired
    private ITInWarehouseService tInWarehouseService;

    /**
     * 查询备件入库列表
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:history:in')")
    @GetMapping("/list")
    public TableDataInfo list(TInWarehouse tInWarehouse) {
        startPage();
        List<TInWarehouse> list = tInWarehouseService.selectTInWarehouseList(tInWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出备件入库列表
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:history:in:export')")
    @Log(title = "备件入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TInWarehouse tInWarehouse) {
        List<TInWarehouse> list = tInWarehouseService.selectTInWarehouseList(tInWarehouse);
        ExcelUtil<TInWarehouse> util = new ExcelUtil<>(TInWarehouse.class);
        util.exportExcel(response, list, "备件入库数据");
    }

    /**
     * 获取备件入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:in:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tInWarehouseService.selectTInWarehouseById(id));
    }

    /**
     * 新增备件入库
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:in:add')")
    @Log(title = "备件入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TInWarehouse tInWarehouse) {
        if(tInWarehouse.getOutNum() <= 0L){
            return toAjax1(-1, "入库数量必须大于0！");
        }
        String operName = getUsername();
        int flag = tInWarehouseService.insertTInWarehouse(tInWarehouse, operName);
        if (flag == -1) {
            return toAjax1(flag, "备件不存在");
        }
        return toAjax1(flag, "入库成功");
    }

    /**
     * 修改备件入库
     */
    @PreAuthorize("@ss.hasPermi('wms:in:edit')")
    @Log(title = "备件入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TInWarehouse tInWarehouse) {
        return toAjax(tInWarehouseService.updateTInWarehouse(tInWarehouse));
    }

    /**
     * 删除备件入库
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:history:in:remove')")
    @Log(title = "备件入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tInWarehouseService.deleteTInWarehouseByIds(ids));
    }
}
